/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager.media;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class Environment {

    private URI envtype;

    private List<AnyObject> any;

    public Environment() {
        any = new ArrayList<AnyObject>();
    }

    public List<AnyObject> getAny() {
        return any;
    }

    public URI getEnvtype() {
        return envtype;
    }

    public void setEnvtype(URI value) {
        this.envtype = value;
    }

    public static Environment marshalFrom(KXmlParser parser) throws IOException {
        if (parser == null) {
            throw new IllegalArgumentException("parser");
        }

        Environment environment = new Environment();
        try {
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "environment");
            String envtype = parser.getAttributeValue(null, "envtype");
            URI uri;
            try {
                uri = new URI(envtype);
            } catch (URISyntaxException e) {
                throw new IOException(e.getMessage());
            }
            environment.setEnvtype(uri);

            while (parser.nextTag() == XmlPullParser.START_TAG) {
                AnyObject o = new AnyObject();
                o.setName(parser.getName());
                o.setValue(parser.nextText());
                environment.getAny().add(o);
            }

        } catch (XmlPullParserException e) {
            throw new IOException("JV-000-010:" + e.toString());
        }
        return environment;
    }
}
